#!/usr/bin/env python
from google.appengine.ext import blobstore
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.api import memcache
from google.appengine.api.capabilities import CapabilitySet

class DataUser(db.Model):
    email = db.EmailProperty(required=True)
    user = db.UserProperty(required=True)
    credit = db.FloatProperty()
    created = db.DateTimeProperty(auto_now_add=True)
    modified = db.DateTimeProperty(auto_now=True)
    lastLogin = db.DateTimeProperty(auto_now_add=True)
    image_blob_key = blobstore.BlobReferenceProperty()
    about_me = db.TextProperty()
    
    @classmethod
    def get_current_user(cls):
        #print "User-" + users.get_current_user().user_id()
        if users.get_current_user() is None:
            return None
        #if CapabilitySet('memcache').is_enabled():
        #    u = memcache.get("User-" + users.get_current_user().user_id())
        #    if u is None:
        u = DataUser.get_by_key_name(users.get_current_user().email())

        if u is None:
                u = DataUser(key_name=users.get_current_user().email(),
                         user=users.get_current_user(),
                         email=users.get_current_user().email(),
                         credit=100.0)
                base_dir = Directory(parent=u,
                                     name='/',
                                     description = '',
                                     password = '')
                u.put()
                base_dir.put()
        #    memcache.add('User-' + users.get_current_user().user_id(), u)
        return u
    
class Directory(db.Model):
    name = db.StringProperty()
    description = db.StringProperty(multiline=True)
    password = db.StringProperty()
    deleted = db.BooleanProperty(default=False)
    directory = db.SelfReferenceProperty(collection_name='directories')
    
class File(db.Model):
    name = db.StringProperty()
    description = db.StringProperty(multiline=True)
    blob_info = blobstore.BlobReferenceProperty()
    deleted = db.BooleanProperty(default=False)
    thumbnail = db.StringProperty()
    directory = db.ReferenceProperty(Directory, collection_name='files')